package com.telkomsigma.spring.hibernate.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.telkomsigma.spring.hibernate.persistence.domain.FootballTeam;
import com.telkomsigma.spring.hibernate.persistence.domain.Player;
import com.telkomsigma.spring.hibernate.persistence.domain.TPlayer;
import com.telkomsigma.spring.hibernate.persistence.domain.TeamCapt;
import com.telkomsigma.spring.hibernate.service.CaptainService;
import com.telkomsigma.spring.hibernate.service.PlayerService;
import com.telkomsigma.spring.hibernate.service.TeamService;

@Controller
@SessionAttributes
public class CaptainController {

	ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
	TeamService teamDao = (TeamService) context.getBean("serviceTeam");
	PlayerService playerDao = (PlayerService) context.getBean("playerService");
	CaptainService captainDao = (CaptainService) context.getBean("captainService");

	FootballTeam team = new FootballTeam();
	Player player = new Player();

	@RequestMapping(value = "/showCaptain", method = RequestMethod.GET)
	public ModelAndView showCaptain(@RequestParam("teamId") int teamId) {
		team = teamDao.findById(teamId);
		List<TPlayer> playerList = team.getCaptains();
		Map<String, Object> model = new HashMap<String, Object>();

		Iterator i = playerList.iterator();
		List<Player> players = new ArrayList<Player>();


		model.put("team",team);
		model.put("players", playerList);

		return new ModelAndView("showCaptain","model",model);
	}
	//	
	@RequestMapping(value = "/saveCaptain", method = RequestMethod.POST)
	protected ModelAndView saveCaptain(@RequestParam("teamId") String teamId,
			@RequestParam("playerId") String playerId) {

		FootballTeam team = teamDao.findById(Integer.parseInt(teamId));
		List<TPlayer> list = team.getCaptains();
		TPlayer player = playerDao.findById(Integer.parseInt(playerId));
		list.add(player);
		team.setCaptains(list);
		teamDao.update(team);

		FootballTeam team2 = teamDao.findById(Integer.parseInt(teamId));
		Map<String, Object> model = new HashMap<String, Object>();
		List<TPlayer> list4 = team.getCaptains();

		model.put("players", list4);
		model.put("team", team2);

		return new ModelAndView("showCaptain", "model", model);

	}

	@RequestMapping(value = "/addCaptain", method = RequestMethod.GET)
	public ModelAndView addCaptain (@RequestParam("teamid") int teamId) {

		FootballTeam team = teamDao.findById(teamId);

		List<TPlayer> captain = team.getCaptains();
		List<TPlayer> list = playerDao.findByProperty("footballTeam", team, 0);

		Map<String, Object> list3 = new HashMap<String, Object>();

		list3.put("player", list);
		list3.put("team2", team);

		return new ModelAndView("newCaptain", "list3", list3);
	}

	@RequestMapping(value = "/deleteCaptain", method = RequestMethod.GET)
	protected ModelAndView deleteCaptain(@RequestParam("teamId") String teamId,
			@RequestParam("playerId") String playerId) {
		TeamCapt captain = captainDao.findById(Integer.parseInt(playerId));
		captainDao.remove(captain);

		FootballTeam team = teamDao.findById(Integer.parseInt(teamId));
		Map<String, Object> model = new HashMap<String, Object>();
		List<TPlayer> list = team.getCaptains();

		model.put("players", list);
		model.put("team", team);

		return new ModelAndView("showCaptain", "model", model);
	}
}
